# -*- coding: utf-8 -*-

import unicodedata

from Products.ZCTextIndex.ISplitter import ISplitter
from Products.ZCTextIndex.PipelineFactory import element_factory
from Products.CMFPlone.utils import classImplements
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.utils import getSiteEncoding

from core import bigrams, kananormalize
from config import rx_U, rxGlob_U
from config import DEBUG

def getenc(self):
    return getSiteEncoding(self)
    # portal_properties = getToolByName(self, 'portal_properties')
    # site_properties = getattr(portal_properties,'site_properties', None)
    # if site_properties is None:
    #     return 'UTF-8'
    # enc = site_properties.getProperty('default_charset', 'UTF-8')
    # return enc

def processGlob_u(self, lst):
    enc = getenc(self)
    return processGlob_core(lst, enc)

def process_u(self, lst):
    enc = getenc(self)
    return process_core(lst, enc)

def ulist_kananormalize(ulst):
    return [kananormalize(x) for x in ulst]

def ulist_normalize(ulst):
    return [unicodedata.normalize('NFKC', x) for x in ulst]

def getulst(ulst):
    xs = findalls(ulist_normalize(ulst), rx_U)
    return bigrams(xs, False)

def getulst_glob(ulst):
    xs = findalls(ulist_normalize(ulst), rxGlob_U)
    return bigrams(xs, True)


def process_core(lst, enc):
    ulst = decodes(lst, enc)
    ulstr = getulst(ulst)
    return encodes(ulstr, enc)
    #return encodes(ulist_kananormalize(ulstr), enc)

def processGlob_core(lst, enc):
    ulst = decodes(lst, enc)
    ulstr = getulst_glob(ulst)
    return encodes(ulstr, enc)
    #return encodes(ulist_kananormalize(ulstr), enc)

def encodes(lst, enc):
    slst = []
    for x in lst:
        assert isinstance(x, basestring)
        if isinstance(x, unicode):
            x = x.encode(enc, "replace")
        slst.append(x)
    return slst
    # return [x.encode(enc, "replace") for x in lst]

def decodes(lst, enc):
    ulst = []
    for x in lst:
        assert isinstance(x, basestring)
        if not isinstance(x, unicode):
            x = x.decode(enc, "replace")
        ulst.append(x)
    return ulst
    # return [x.decode(enc, "replace") for x in lst]

def findalls(lst, rx):
    return [y for x in lst for y in rx.findall(x)]


def process_post_glob_core(lst, enc):
    if DEBUG:
        print "lst:", lst
    ulst = [u"".join([y for y in x.decode(enc, "replace") if y not in u"?*"]) for x in lst]
    if len(ulst) > 1 and len(ulst[-1]) == 1:
        result = ulst[:-1]
    else:
        result = ulst
    if DEBUG:
        print "process_post_glob",result
        print "--", u",".join(result)
    return result

class BigramSplitter(object):
    """
    """
    meta_type = 'BigramSplitter'
    __implements__ = ISplitter

    def process(self, lst):
        if DEBUG:
            print "process_o", lst
        result = process_u(self, lst)
        if DEBUG:
            print "--",u",".join(result)    
        return result
    
    def process_post_glob(self, lst):
        enc = getenc(self)
        return process_post_glob_core(lst, enc)

    def processGlob(self, lst):
        return processGlob_u(self, lst)

classImplements(BigramSplitter, BigramSplitter.__implements__)

try:
    element_factory.registerFactory('Word Splitter',
        'Bigram Splitter', BigramSplitter)
except ValueError:
    # In case the splitter is already registered, ValueError is raised
    pass


class BigramCaseNormalizer(object):

    def process(self, lst):
        enc = getenc(self)
        result = []
        for s in lst:
            try:
                if not isinstance(s, unicode):
                    s = unicode(s, enc)
            except (UnicodeDecodeError, TypeError):
                result.append(s.lower())
            else:
                normaed = unicodedata.normalize('NFKC', s)
                # kana_normaed = kananormalize(normaed)
                # result.append(kana_normaed.lower().encode(enc, 'replace'))
                result.append(normaed.lower().encode(enc, 'replace'))
        return result

try:
    element_factory.registerFactory('Case Normalizer',
        'Bigram Case Normalizer', BigramCaseNormalizer)
except ValueError:
    # In case the normalizer is already registered, ValueError is raised
    pass
